package com.woke.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.woke.data.BankCard;
import com.zhongcai.online.BR;
import com.zhongcai.online.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewProvider;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/29
 * 备注：
 */
public class BankCardsViewProvider extends ItemViewProvider<BankCard, BankCardsViewProvider.ViewHolder> {

    private OnItemClickListener mItemClickListener;

    public BankCardsViewProvider(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.adapter_bankcard_item, parent, false);
        ViewHolder holder = new ViewHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final BankCard bankCard) {
//        String[] bankNames = holder.itemView.getResources().getStringArray(R.array.bank_5);
//        if (bankCard != null) {
//            if (!TextUtils.isEmpty(bankCard.getBrname())) {
//                if (bankCard.getBrname().contains(bankNames[0])) {
//                    //工商银行
//                    holder.itemView.setBackgroundResource(R.drawable.s_icbc_bg);
//                } else if (bankCard.getBrname().contains(bankNames[1])) {
        //建设银行
        holder.itemView.setBackgroundResource(R.drawable.s_ccb_bg);
//                } else if (bankCard.getBrname().contains(bankNames[2])) {
//                    //中国银行
//                    holder.itemView.setBackgroundResource(R.drawable.s_boc_bg);
//                } else if (bankCard.getBrname().contains(bankNames[3])) {
//                    //农业银行
//                    holder.itemView.setBackgroundResource(R.drawable.s_abc_bg);
//                } else if (bankCard.getBrname().contains(bankNames[4])) {
//                    //交通银行
//                    holder.itemView.setBackgroundResource(R.drawable.s_ccb_bg);
//                } else {
//                    holder.itemView.setBackgroundResource(R.drawable.s_comm_bg);
//                }
//            } else {
//                holder.itemView.setBackgroundResource(R.drawable.s_ccb_bg);
//            }
        holder.getBinding().setVariable(BR.bankCard, bankCard);
//        }

        RxView.clicks(holder.itemView).throttleFirst(2, TimeUnit.SECONDS).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                if (mItemClickListener != null) {
                    mItemClickListener.OnItemClick(holder.itemView, bankCard, getPosition(holder));
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;


        ViewHolder(View itemView) {
            super(itemView);
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }
}
